﻿<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<div id="updateDialog" class="container-fluid">
	<div class="row">
		<form id="updateForm" class="form-horizontal" method="post">
			<div class="form-group">
				<label class="col-sm-4 control-label" for="username">
					<span class="help-inline"><font color="red">*</font> </span>
					帐号：</label>
				<div class="col-sm-5">
					<input type="text" class="form-control" id="username" name="username" placeholder="帐号" maxlength="20" value="${user.username}" readonly/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-4 control-label" for="realname">
					<span class="help-inline"><font color="red">*</font> </span>
					姓名：</label>
				<div class="col-sm-5">
					<input type="text" class="form-control" id="realname" name="realname" placeholder="姓名" maxlength="20" value="${user.realname}"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-4 control-label" for="agent" >
					<span class="help-inline"><font color="red">*</font> </span>
					所属代理：</label>
				<div class="col-sm-5">
					<div id="agent" data-id="${agent.id}" data-name="${agent.name}" ></div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-4 control-label" for="avatar" value="${user.avatar}">
					头像：</label>
				<div class="col-sm-5">
					<input type="text" class="form-control" id="avatar" name="avatar" placeholder="头像" maxlength="150"/>
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-5 col-sm-offset-4">
					<div id="picker">上传头像</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-4 control-label" for="phone">
					<span class="help-inline"><font color="red">*</font> </span>
					电话：</label>
				<div class="col-sm-5">
					<input type="text" class="form-control" id="phone" name="phone" placeholder="电话" maxlength="20" value="${user.phone}"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-4 control-label" for="email">
					<span class="help-inline"><font color="red">*</font> </span>
					邮箱：</label>
				<div class="col-sm-5">
					<input type="text" class="form-control" id="email" name="email" placeholder="邮箱" maxlength="50" value="${user.email}"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-4 control-label">
					<span class="help-inline"><font color="red">*</font> </span>
					性别：</label>
				<div class="col-sm-5">
					<div class="radio" class="form-control">
						<div class="radio radio-inline radio-info">
							<input id="sex_1" type="radio" name="sex" value="1" <c:if test="${user.sex==1}">checked</c:if>>
							<label for="sex_1">男 </label>
						</div>
						<div class="radio radio-inline radio-danger">
							<input id="sex_0" type="radio" name="sex" value="0" <c:if test="${user.sex==0}">checked</c:if>>
							<label for="sex_0">女 </label>
						</div>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-4 control-label">
					<span class="help-inline"><font color="red">*</font> </span>
					状态：</label>
				<div class="col-sm-5">
					<div class="radio">
						<div class="radio radio-inline radio-success">
							<input id="locked_0" type="radio" name="locked" value="0"  <c:if test="${user.locked==0}">checked</c:if>>
							<label for="locked_0">正常 </label>
						</div>
						<div class="radio radio-inline">
							<input id="locked_1" type="radio" name="locked" value="1"<c:if test="${user.locked==1}">checked</c:if>>
							<label for="locked_1">锁定 </label>
						</div>
					</div>
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-8 col-sm-offset-4">
					<button type="button" class="btn btn-primary" onclick="createSubmit();">
						保存
					</button>
					<button type="button" class="btn btn-default"
							onclick="updateDialog.close();">取消
					</button>
				</div>
			</div>
		</form>
	</div>
</div>
<script>
    $(function(){
        //初始化树形select控件
        <%--$("#agent").bootstrapCombotree({--%>
            <%--data: ${upmsAgents},//data应符合实例的data格式--%>
            <%--defaultLable:"请选择",--%>
            <%--name : 'agentId'//combotree值域的name，可以用在表单提交--%>
        <%--})--%>
        $("#agent").ztreeSelect({
            name:"agentId",
            url: '${basePath}/manage/agentTree',
            params: {isLeaf: 1},
            renderId: "agent",
            callback: function (nodeIds, treeNode) {
            }
        })
    });
    function initUploader() {
        //百度上传按钮
        var uploader = WebUploader.create({
            // swf文件路径
            swf: '${basePath}/resources/zheng-admin/plugins/webuploader-0.1.5/Uploader.swf',
            // 文件接收服务端
            method: 'POST',
            // 选择文件的按钮。可选。
            // 内部根据当前运行是创建，可能是input元素，也可能是flash.
            pick: {
                "id": '#picker',
                "multiple": false
            },
            // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
            resize: false,
            // 选完文件后，是否自动上传。
            auto: false,
            // 只允许选择图片文件
            accept: {
                title: '图片文件',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/*'
            }
        });
        uploader.on( 'fileQueued', function(file) {
            $.ajax({dataType: 'json',
                url: '${ZHENG_OSS_ALIYUN_OSS_POLICY}',
                type: 'GET',
                dataType: 'jsonp',
                jsonp: 'callback',
                success: function(result) {
                    var suffix = get_suffix(file.name);
                    var random_name = random_string();
                    uploader.options.formData.key = result.dir + '/' + random_name + suffix;
                    uploader.options.formData.policy = result.policy;
                    uploader.options.formData.OSSAccessKeyId = result.OSSAccessKeyId;
                    uploader.options.formData.success_action_status = 200;
                    uploader.options.formData.callback = result.callback;
                    uploader.options.formData.signature = result.signature;
                    uploader.options.server = result.action;
                    uploader.upload();
                },
                error: function(msg) {
                    console.log(msg);
                }
            });
        });
        uploader.on( 'uploadSuccess', function(file, response) {
            $('#avatar').val(response.data.filename).focus();
        });
        uploader.on( 'uploadError', function(file) {
            console.log('uploadError', file);
        });
    }
    // 根据路径获取后缀
    function get_suffix(filename) {
        var pos = filename.lastIndexOf('.');
        var suffix = '';
        if (pos != -1) {
            suffix = filename.substring(pos);
        }
        return suffix;
    }
    // 随机字符串
    function random_string(len) {
        len = len || 32;
        var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        var maxPos = chars.length;
        var pwd = '';
        for (i = 0; i < len; i++) {
            pwd += chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return pwd;
    }
    function createSubmit() {
        $.ajax({dataType: 'json',
            type: 'post',
            url: '${basePath}/manage/user/update/${user.userId}',
            data: $('#updateForm').serialize(),
            beforeSend: function() {
                if ($('#username').val() == '') {
                    $('#username').focus();
                    return false;
                }
            },
            success: function(result) {
                if (result.code != 1) {
                    if (result.data instanceof Array) {
                        $.each(result.data, function(index, value) {
                            $.confirm({
                                theme: 'dark',
                                animation: 'rotateX',
                                closeAnimation: 'rotateX',
                                title: false,
                                content: value.errorMsg,
                                buttons: {
                                    confirm: {
                                        text: '确认',
                                        btnClass: 'waves-effect waves-button waves-light'
                                    }
                                }
                            });
                        });
                    } else {
                        $.confirm({
                            theme: 'dark',
                            animation: 'rotateX',
                            closeAnimation: 'rotateX',
                            title: false,
                            content: result.data,
                            buttons: {
                                confirm: {
                                    text: '确认',
                                    btnClass: 'waves-effect waves-button waves-light'
                                }
                            }
                        });
                    }
                } else {
                    updateDialog.close();
                    $table.bootstrapTable('refresh');
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                $.confirm({
                    theme: 'dark',
                    animation: 'rotateX',
                    closeAnimation: 'rotateX',
                    title: false,
                    content: textStatus,
                    buttons: {
                        confirm: {
                            text: '确认',
                            btnClass: 'waves-effect waves-button waves-light'
                        }
                    }
                });
            }
        });
    }
</script>